package com.huluobo.high3demo.main;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.Manifest;
import android.content.Intent;
import android.view.MenuItem;

import com.google.android.material.navigation.NavigationBarView;
import com.huluobo.high3demo.R;
import com.huluobo.high3demo.adapter.CommonPagerAdapter;
import com.huluobo.high3demo.databinding.ActivityMainBinding;
import com.huluobo.high3demo.home.HomeFragment;
import com.huluobo.high3demo.fragment.MessageFragment;
import com.huluobo.high3demo.mine.MineFragment;
import com.huluobo.high3demo.video.ReCommendFragment;
import com.huluobo.high3demo.startLive.StartLiveActivity;
import com.huluobo.mvvm.view.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private CommonPagerAdapter pagerAdapter;

    private List<Fragment> fragmentList = new ArrayList<>();

    private MenuItem selectedMenu;

    @Override
    protected void initData() {
        fragmentList.clear();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new ReCommendFragment());
        fragmentList.add(new MessageFragment());
        fragmentList.add(new MineFragment());

        pagerAdapter = new CommonPagerAdapter(this, fragmentList);
        dataBinding.mainVp2.setAdapter(pagerAdapter);

        requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA,
                Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);

    }

    @Override
    protected void initView() {
        //注册动态权限
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE}, 101);

        dataBinding.mainNav.setItemActiveIndicatorEnabled(false);
        dataBinding.mainVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        dataBinding.mainNav.getMenu().getItem(0).setChecked(true);
                        break;
                    case 1:
                        dataBinding.mainNav.getMenu().getItem(1).setChecked(true);
                        break;
                    case 2:
                        dataBinding.mainNav.getMenu().getItem(3).setChecked(true);
                        break;
                    case 3:
                        dataBinding.mainNav.getMenu().getItem(4).setChecked(true);
                        break;

                }
            }
        });

        dataBinding.mainNav.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                selectedMenu = item;
                switch (selectedMenu.getItemId()) {
                    case R.id.main_tab_home:
                        dataBinding.mainVp2.setCurrentItem(0);
                        break;

                    case R.id.main_tab_video:
                        dataBinding.mainVp2.setCurrentItem(1);
                        break;

                    case R.id.main_tab_start:
                        Intent intent = new Intent(MainActivity.this, StartLiveActivity.class);
                        startActivity(intent);
                        break;

                    case R.id.main_tab_message:
                        dataBinding.mainVp2.setCurrentItem(2);
                        break;

                    case R.id.main_tab_mine:
                        dataBinding.mainVp2.setCurrentItem(3);
                        break;
                }

                return true;
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
}